<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/utilities/xajax/xajax_core/xajax.inc.php';	
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/gui/presenter/PresenterResource.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/FirePHPCore-0.3.2/lib/FirePHPCore/FirePHP.class.php';
		
	$debug = FirePHP :: getInstance(true);
	$debug -> setEnabled(true);
	
	//Permite eliminar los notices
	error_reporting (E_ALL ^ E_NOTICE);
	
	$_SESSION['debug'] = $debug;
	
	$xajax = new xajax(); 
	 
	$xajax -> register(XAJAX_FUNCTION, 'clickCreateResourcesRequest');
	$xajax -> register(XAJAX_FUNCTION, 'setLabels');
	
	$xajax -> processRequest(); 
	 
	$xajax -> configure('javascript URI','../../../utilities/xajax/');
	
	/**
	 * Method to execute to create a new resources request
	 */
	function clickCreateResourcesRequest($resources, $requester) {
		$presenterResource = new PresenterResource();
		
		$resourcesRequestCreation = $presenterResource -> clickCreateResourcesRequest($resources, $requester);
		
		$response = new xajaxResponse();
		
		if ($resourcesRequestCreation == 0)
			$response -> assign("transactionResult", "innerHTML", 
				'<font size="+1" color="#FF0000">La operaci&oacute;n fall&oacute;</font>');
		
		else
			$response -> assign("transactionResult", "innerHTML", 
				'<font size="+1" color="#006600">Operaci&oacute;n exitosa</font>');
		
		return $response;
	}
	
	/**
	 * Code to execute to set labels
	 */
	function setLabels($category) {
		switch ($category) {
			case 'MATERIALES':
				$idLabel = 'Nombre';
				$costLabel = 'Costo';
			break;
			
			case 'VIATICOS':
				$idLabel = 'Concepto';
				$costLabel = 'Costo';
			break;
			
			case 'EMPLEADOS':
				$idLabel = 'C&eacute;dula';
				$costLabel = 'Sal/Sem';
			break;
			
			default:
				$idLabel = 'Nombre';
				$costLabel = 'Costo';
			break;
		}
		
		$response = new xajaxResponse();
		$response -> assign("idLabel", "innerHTML", $idLabel);
		$response -> assign("costLabel", "innerHTML", $costLabel);
		return $response;
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registre una nueva Orden de Compra</title>
<?php 
	$xajax->printJavascript("../../../utilities/xajax/"); 
?>
<link rel="stylesheet" type="text/css" href="../css/formsStyles.css"/>
<link rel="stylesheet" type="text/css" href="../css/styles2.css" />

<!-- include the Tools -->
<script src="http://cdn.jquerytools.org/1.2.6/full/jquery.tools.min.js"></script>

<script language="javascript">
	function gup( name ){
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp ( regexS );
		var tmpURL = window.location.href;
		var results = regex.exec( tmpURL );
		if( results == null )
			return"";
		else
			return results[1];
	}
	
	function addResource(category, identifier, specifications, cost) {
		var resources = document.resource.resources.value;
		
		if (resources != '')
			resources += '\n';
			
		resources += category + ', ' + identifier;
		
		if (specifications != '')
			resources += ' (' + specifications + ')';
			
		resources += ', ' + cost + ' Bs.';
			
		document.resource.resources.value = resources;
	}
</script>

</head>
<body>
	<div id="formsContainer">
        <div id="title">Registre una nueva solicitud de recursos</div>
        <br />
        <div id="subtitle">Indique los recursos a solicitar</div>
        <form name="resource" id="resource">
        	<table class="formTable" cellspacing="0" cellpadding="5">
                <tr class="odd">
                    <td align="right">Categor&iacute;a</td>
                    <td align="left">
                        <select name="category" onchange="xajax_setLabels(document.resource.category.value)">
                        	<option value="0">Seleccione una categor&iacute;a</option>
                            <option value="MATERIALES">MATERIALES</option>
                            <option value="VIATICOS">VI&Aacute;TICOS</option>
                            <option value="EMPLEADOS">EMPLEADOS</option>
                        </select>
                    </td>
                    
                    <td align="right"><div id="idLabel">Nombre</div></td>
                    <td align="left">
                    	<input type="text" name="identifier" size="35" maxlength="40" />
                    </td>
                </tr>
                
                <tr class="even">
                	<td align="right">Especificaciones</td>
                    <td align="left">
                    	<input type="text" name="specifications" size="40" maxlength="200" />
                    </td>
                    
                    <td align="right"><div id="costLabel">Costo</div></td>
                    <td align="left">
                    	<input type="text" name="cost" size="30" maxlength="8" />&nbsp;Bs.
                    </td>
                	
                    <td align="left">
                    	<input type="button" value="Agregar" onclick="addResource(document.resource.category.value, document.resource.identifier.value, document.resource.specifications.value, document.resource.cost.value)" />
                    </td>
                </tr>
                
                <tr class="odd">
                	<td align="right" valign="top">Recursos</td>
                    <td align="left" colspan="4">
                    	<textarea name="resources" cols="64" rows="5"></textarea>
                    </td>
                </tr>
       		</table>
            
        <div id="transactionResult" align="center"></div>
            
        <br /><div id="buttons" align="center">
            <input type="button" name="buttonCreateResourcesRequest" value="Registrar" 
                onclick="xajax_clickCreateResourcesRequest(document.resource.resources.value, gup('username'))" />
        </div>        
        </form>
    </div>
</body>
</html>